//
//  SocialPhotoBrowserViewController.m
//  ShowYork
//
//  Created by hxh on 2020/9/23.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "DynamicPhotoBrowserViewController.h"
#import "SocialPhotoBrowserCell.h"
@interface DynamicPhotoBrowserViewController ()<UICollectionViewDelegate,UICollectionViewDataSource,UIScrollViewDelegate>
@property (nonatomic,strong)UICollectionView *collectionView;
@property (nonatomic,strong)UIView *toolView;
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UIButton *backBtn;
@property (nonatomic,strong)UIButton *removeBtn;
@property (nonatomic,assign)NSInteger currentIndex;

@property (nonatomic,assign)BOOL isShow;

@property (nonatomic,strong)NSMutableArray *sourceArray;
@end

@implementation DynamicPhotoBrowserViewController

- (NSMutableArray *)sourceArray{
    if (!_sourceArray) {
        _sourceArray = [NSMutableArray array];
    }
    return _sourceArray;
}

- (instancetype)initWithDataArray:(NSMutableArray *)dataArray andSelectedIndex:(NSInteger)selectedIndex{
    if (self = [super init]) {
        [self.sourceArray addObjectsFromArray:dataArray];
        self.currentIndex = selectedIndex;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.isHideNavBar = YES;
    
    self.view.backgroundColor = [UIColor blackColor];
    

    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.sectionInset = UIEdgeInsetsZero;
    layout.minimumLineSpacing = 0;
    layout.minimumInteritemSpacing = 0;
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    layout.itemSize = CGSizeMake(kScreenW, kScreenH);
    
    self.collectionView = [[UICollectionView alloc]initWithFrame:self.view.bounds collectionViewLayout:layout];
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    self.collectionView.pagingEnabled = YES;
    [self.view addSubview:self.collectionView];
    
    [self.collectionView registerClass:[SocialPhotoBrowserCell class] forCellWithReuseIdentifier:@"SocialPhotoBrowserCell"];
    
    
    self.toolView = [[UIView alloc]init];
    self.toolView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.toolView];
    [self.toolView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(-kNavBarHeight);
        make.left.right.equalTo(self.view);
        make.height.mas_equalTo(kNavBarHeight);
    }];
    
    [self setToolSubViews];
    
    
    [self.collectionView setContentOffset:CGPointMake(kScreenW * self.currentIndex, 0) animated:YES];
    self.titleLb.text = [NSString stringWithFormat:@"%lu/%lu",self.currentIndex+1,self.sourceArray.count];
}

- (void)setToolSubViews{
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.font = [UIFont boldSystemFontOfSize:18];
    self.titleLb.textColor = RGBA(17, 17, 17, 1);

    self.backBtn = [[UIButton alloc]init];
    [self.backBtn setImage:[UIImage imageNamed:@"common_back_tag"] forState:UIControlStateNormal];
    [self.backBtn addTarget:self action:@selector(backMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.toolView addSubview:self.backBtn];
    
    self.removeBtn = [[UIButton alloc]init];
    [self.removeBtn setImage:[UIImage imageNamed:@"social_tag_remove"] forState:UIControlStateNormal];
    [self.removeBtn addTarget:self action:@selector(removeMethod) forControlEvents:UIControlEventTouchUpInside];
    [self.toolView addSubview:self.removeBtn];
    
    
    [self.toolView addSubview:self.titleLb];
    
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.toolView);
        make.bottom.equalTo(self.toolView).offset(-8);
    }];
        
    [self.backBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.toolView).offset(16);
        make.bottom.equalTo(self.toolView).offset(-8);
    }];
    [self.removeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.toolView.mas_right).offset(-16);
        make.bottom.equalTo(self.toolView).offset(-8);
    }];
}


- (void)backMethod{
        
    [self.navigationController popViewControllerAnimated:YES];
    
}

- (void)removeMethod{
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"确定要删除这张照片？" message:nil preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *confirmAction = [UIAlertAction actionWithTitle:@"删除" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [alertController dismissViewControllerAnimated:YES completion:nil];
        NSInteger index = self.collectionView.contentOffset.x/kScreenW;
        [self.sourceArray removeObjectAtIndex:index];
        [self.collectionView reloadData];
        
        if (self.sourceArray.count == 1) {
            [self.collectionView scrollToLeftAnimated:NO];
        }else if(self.sourceArray.count > 1){
            NSIndexPath *indexPath = [NSIndexPath indexPathForItem:index-1 inSection:0];
            [self.collectionView scrollToItemAtIndexPath:indexPath atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
        }
        self.titleLb.text = [NSString stringWithFormat:@"%lu/%lu",index == 0?1:index,self.sourceArray.count];
        [WHToast showMessage:@"已删除" duration:2 finishHandler:nil];
        if (self.done) {
            self.done(index);
        }
        if (self.sourceArray.count == 0) {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        [alertController dismissViewControllerAnimated:YES completion:nil];
    }];
    [confirmAction setValue:[RPAppManager themColor] forKey:@"titleTextColor"];
    [cancelAction setValue:[RPAppManager themColor] forKey:@"titleTextColor"];
             //往alertViewController上添加alertAction
    [alertController addAction:confirmAction];
    [alertController addAction:cancelAction];
    [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alertController animated:YES completion:nil];
}

- (void)showAnimation{
    
    CGFloat height = 0;
    
    if (self.isShow) {
        height = 0;
    }else{
        height = -kNavBarHeight;
    }

    [self.toolView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(height);
    }];
   
}


#pragma mark - collectioViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.sourceArray.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    SocialPhotoBrowserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SocialPhotoBrowserCell" forIndexPath:indexPath];
    [cell setContainImg:self.sourceArray[indexPath.item]];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    self.isShow = !self.isShow;

    [self showAnimation];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSInteger index = scrollView.contentOffset.x/kScreenW;
    self.titleLb.text = [NSString stringWithFormat:@"%lu/%lu",index+1,self.sourceArray.count];
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    self.isShow = NO;
    
    [self showAnimation];
}

@end
